<?php

/**
 * Date: 2015-09-09
 */

namespace Admin\Controller;

use \Think\Controller;

class SjController extends Controller {

    // 会员利息计算 日结算
    public function sj_day() {
        //dump('1111');exit;
        header('Content-Type:text/html;Charset=UTF-8');
        $list = M('account_money_log')->where('type=0 and is_type = 1')->select(); //查出正在分红的信息
        if (!empty($list)) {
            foreach ($list as $v) {
                //判断分红的天数是否大于或等于分红出局天数，如果是就出局
                if ($v['price'] >= $v['money']) {
                    M('account_money_log')->where('id=' . $v['id'])->save(array('type' => 9, 'out_time' => time()));
                    continue;
                } else {
                    if (strtotime($v['zf_time']) + tpCache('shopping.lx_num') * 24 * 3600 < time()) {
                        
                        $level = M('users')->where(array('user_id' => $v['user_id']))->getField('level');
                        $per = M('user_level')->where(array('level_id' => $level))->getField('price6_per');
                        $rlx = $v['money'] * $per / 100; //周期分红利息
                        $num = $v['num'] + 1;
                        $lx_price = $v['price'] + $rlx;
                        if ($v['type'] != 9) {
                            $user = M('users')->where(array('user_id' => $v['user_id']))->field('account,first_leader,is_monye')->find();
                            if ($user['is_monye'] == 1) {
                                //dump($v);exit;
                                $tgbza = M('account_money_log')->where(array('id' => $v['id']))->save(array('num' => $num, 'price' => $lx_price, 'zf_time' => time())); //添加每天分红天数
                                if (tpCache('shopping . minus_1') > 0) {
                                    $fund_money = $rlx * tpCache('shopping.minus_1') / 100;  // 算出当笔奖金的储备基金
                                } else {
                                    $fund_money = 0;
                                }
                                $user_money = $rlx - $fund_money;    // 扣掉储备基金 实际进账奖金
                                accountLog($v['user_id'], '+' . $user_money, 0, 0, '+' . $fund_money, 0, 0, '静态分红');
                                accoutnt_day($v['user_id'], 0, 0, 0,'+' . $user_money, 0, '+' . $fund_money, 0, 0, 0, '+' .$rlx);
                                clear_pyj($user['first_leader'], $rlx,$user['account'] .'静态奖');  // 计算动态奖
                                bochu_day_log(0, $rlx);
                            }
                        } else {
                            M('account_money_log')->where(array('id' => $v['id']))->save(array('type' => 9, 'out_time' => time())); //强制出局
                        }
                        echo "<script>alert('分红成功');window.location.href='" . U('Index/welcome') . "';</script>";
                    }
                }
            }
            echo "<script>alert('暂无分红数据');window.location.href='" . U('Index/welcome') . "';</script>";
        } else {
            echo "<script>alert('暂无分红数据');window.location.href='" . U('Index/welcome') . "';</script>";
        }
    }

    // 月结算
    public function sj_month() {
        header('Content-Type:text/html;Charset=UTF-8');
        $tgbz = M('users');
        $tgbz_list = $tgbz->where(array('money_total' => tpCache('web_info.shiji_5')))->field('user_id,money_total,statu_money1')->select(); //查出正在分红的信息
        if (!empty($tgbz_list)) {
            foreach ($tgbz_list as $v) {
                $num = count($tgbz_list);
                $bcmap['zf_time'] = array(between, array(date('Y-m-01', strtotime('-1 month')), date('Y-m-t', strtotime('-1 month')))); //查当一个月的时间
                $yj = M('account_bochu')->where($bcmap)->sum('shuoru');
                $money = $yj * tpCache('web_info.shiji_6') / 100 / count($tgbz_list);
                $total = M('account_total_log')->where(array('user_id' => $v['user_id']))->field('money3')->find();
                if ($v['statu_money1'] == 1) {
                    if ($v['money_total'] * tpCache('web_info.shiji_7') < $total) {

                        $money5 = $money * tpCache('web_info.pd_sj_4') / 100; //投资积分
                        $money4 = $money * tpCache('web_info.pd_sj_5') / 100; //消费积分

                        accountLog($v['user_id'], 0, 0, 0, '+' . $money4, '+' . $money5, 0, 0, '全球业绩加权分红');
                        accoutnt_day($v['user_id'], '+' . $money, 0, 0, '+' . $money); // 日收入与总收入明细
                        bochu_day_log(0, $money);

                        echo "<script>alert('分红成功');window.location.href='" . U('Index/welcome') . "';</script>";
                    }
                }
                echo "<script>alert('暂无分红数据');window.location.href='" . U('Index/welcome') . "';</script>";
            }
        }
    }

    /**
     * 领导奖
     * 1、个人工资达到50万可以公司的业绩分红2%
     * 2、带出3个领导每个人工资到达50万可以拿公司业绩分红3%
     * 3、带出5个领导每个人工资达到50万可以拿公司业绩分红4%
     * 4、带出7个领导每个人工资达到50万可以拿公司业绩分红5%
     */
    // 月结算
    public function sj_month_1() {
        $account_total_log = M('account_total_log');
        $user = M('users');
        header('Content-Type:text/html;Charset=UTF-8');
        $user_list = $user->where(array('is_activate' => 1))->field('user_id')->select();
        $firstday = date('Y-m-01', strtotime('-1 months'));
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        $gsyj = M('account_bochu')->where(array('zf_time' => array('between', array(strtotime($firstday), strtotime($lastday)))))->sum('shuoru');
        foreach ($user_list as $v) {
            $lever = M('user_level')->where(array('level_id' => $v['level']))->field('price4_per')->find();
            $ztnum = $user->where(array('first_leader' => $v['user_id']))->count();
            $money_total = $account_total_log->where(array('user_id' => $v['user_id']))->getField('money_total');
            if ($ztnum >= tpCache('cash.ldj4_3')) {
                if ($money_total >= tpCache('cash.ldj4_1')) {
                    $total = $gsyj * tpCache('cash.ldj4_2') / 100;
                    $cxj = $total * $lever['price4_per'] / 100;
                    accountLog($v['user_id'], $total - $cxj, $cxj, 0, '公司业绩分红', 0);
                    account_day($v['user_id'], 0, 0, 0, $total - $cxj, 0, $total - $cxj);
                    bochu_day_log(0, $total);
                }
            } elseif ($ztnum >= tpCache('cash.ldj3_3')) {
                if ($money_total >= tpCache('cash.ldj3_1')) {
                    $total = $gsyj * tpCache('cash.ldj3_2') / 100;
                    $cxj = $total * $lever['price4_per'] / 100;
                    accountLog($v['user_id'], $total - $cxj, $cxj, 0, '公司业绩分红', 0);
                    account_day($v['user_id'], 0, 0, 0, $total - $cxj, 0, $total - $cxj);
                    bochu_day_log(0, $total);
                }
            } elseif ($ztnum >= tpCache('cash.ldj2_3')) {
                if ($money_total >= tpCache('cash.ldj2_1')) {
                    $total = $gsyj * tpCache('cash.ldj2_2') / 100;
                    $cxj = $total * $lever['price4_per'] / 100;
                    accountLog($v['user_id'], $total - $cxj, $cxj, 0, '公司业绩分红', 0);
                    account_day($v['user_id'], 0, 0, 0, $total - $cxj, 0, $total - $cxj);
                    bochu_day_log(0, $total);
                }
            } else {
                if ($money_total >= tpCache('cash.ldj1_1')) {
                    $total = $gsyj * tpCache('cash.ldj1_2') / 100;
                    $cxj = $total * $lever['price4_per'] / 100;
                    accountLog($v['user_id'], $total - $cxj, $cxj, 0, '公司业绩分红', 0);
                    account_day($v['user_id'], 0, 0, 0, $total - $cxj, 0, $total - $cxj);
                    bochu_day_log(0, $total);
                }
            }
        }
        $this->success('操作成功');
    }

}
